﻿using System;
using System.Windows.Forms;
using Map_Creator.XNA;
using Microsoft.Xna.Framework;

namespace Map_Creator.Painters
{
    public class Selection : IPainter
    {
        private Point Origin;
        private Point TopLeft;
        private Point ButtomRight;
        private Point PreviosPosition;
        private Point Size;
        private int TileSize;

        public Selection(int tileSize, Point size = default(Point))
        {
            Size = size;
            TileSize = tileSize;
        }

        public Rectangle Rectangle
        {
            get { return new Rectangle(TopLeft.X,TopLeft.Y, Size.X,Size.Y); }
        }

        private void CalculateSize()
        {
            Size = new Point(ButtomRight.X - TopLeft.X,
                                 ButtomRight.Y - TopLeft.Y);
        }

        /// <summary>
        /// Handles when the mouse button is down inside the selection control
        /// </summary>
        /// <param name="position">the tile where the mouse is</param>
        /// <param name="buttons">Pressed Buttons</param>
        /// <returns>the rectangle to invalidate</returns>
        public Rectangle OnMouseDown(Point position, MouseButtons buttons)
        {
            if (buttons != MouseButtons.Left) return new Rectangle();

            var previosTopLeft = TopLeft;
            var previosButtomRight = ButtomRight;

            Origin = new Point(position.X, position.Y);
            TopLeft = Origin;
            ButtomRight = new Point(TopLeft.X + 1,
                                    TopLeft.Y + 1);

            PreviosPosition = Origin;

            Size = new Point(1, 1);

            var minTopLeft = new Point(Math.Min(TopLeft.X, previosTopLeft.X),
                                       Math.Min(TopLeft.Y, previosTopLeft.Y));

            var maxButtomRight = new Point(Math.Max(ButtomRight.X, previosButtomRight.X),
                                       Math.Max(ButtomRight.Y, previosButtomRight.Y));

            return new Rectangle(minTopLeft.X,
                                 minTopLeft.Y,
                                 (maxButtomRight.X - minTopLeft.X),
                                 (maxButtomRight.Y - minTopLeft.Y));
        }

        /// <summary>
        /// Handles when the mouse moves inside the selection control
        /// </summary>
        /// <param name="Position">the tile where the mouse is</param>
        /// <param name="buttons">Pressed Buttons</param>
        /// <returns>the rectangle to invalidate</returns>
        public Rectangle OnMouseMove(Point Position, MouseButtons buttons)
        {
            if (buttons != MouseButtons.Left) return new Rectangle();
            if (Position == PreviosPosition) return new Rectangle();

            var previosTopLeft = TopLeft;
            var previosSize = Size;

            PreviosPosition = Position;

            TopLeft.X = Math.Min(Origin.X, Position.X);
            TopLeft.Y = Math.Min(Origin.Y, Position.Y);

            ButtomRight.X = Math.Max(Origin.X, Position.X) + 1;
            ButtomRight.Y = Math.Max(Origin.Y, Position.Y) + 1;

            CalculateSize();

            return new Rectangle(Math.Min(TopLeft.X, previosTopLeft.X),
                                 Math.Min(TopLeft.Y, previosTopLeft.Y),
                                 Math.Max(Size.X, previosSize.X),
                                 Math.Max(Size.Y, previosSize.Y));
        }

        public Rectangle OnMouseLeave()
        {
            return new Rectangle();
        }

        public Rectangle OnMouseUp()
        {
            return Rectangle.Empty;
        }

        public void OnDraw(XnaGraphics xnaGraphics, Rectangle destination)
        {
            var rect = new Rectangle(TopLeft.X*TileSize, TopLeft.Y*TileSize, Size.X*TileSize, Size.Y*TileSize);
            rect.Width -= 1;
            rect.Height -= 1;
            xnaGraphics.DrawRectangle(rect, Color.Red);
        }
    }
}